package org.abettor.pushbox.notify;

import org.abettor.pushbox.R;
import org.abettor.pushbox.activity.NetUserMapActivity;
import org.abettor.pushbox.activity.QuesstionMapActivity;
import org.abettor.pushbox.activity2.ShowAnswerMapDetailActivity;
import org.abettor.pushbox.activity2.ShowMapDetailActivity;
import org.abettor.pushbox.activity2.SiteMessageActivity;
import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.model.NotifyBean;
import org.abettor.pushbox.service.UpdateNotifyService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;

/**
 * 进行通知的bean
 * 
 * @author jzh
 * 
 */
public class NotifyUtil {
	/**
	 * 注册定时器
	 * @param context
	 * @param claz
	 */
	@SuppressWarnings("unchecked")
	public static void registerReceiverAlarm(Context context, Class claz) {
		Intent intent = new Intent(context, claz);
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
				SystemClock.elapsedRealtime() + Config.WATCH_ALARM_TIME_OUT, pi);
	}

	/**
	 * 启动更新通知的service
	 * @param context
	 */
	public static void startNotifyService(Context context) {
		// 启动service服务
		Intent in = new Intent();
		in.setClass(context, UpdateNotifyService.class);
		context.startService(in);
	}

	/**
	 * 读取通知的消息
	 * @param bean
	 */
	public static String readNotifyMsg(Context context, NotifyBean bean) {
		switch (bean.getType()) {
		case NotifyBean.UPLOAD_NET_MAP:
			return context.getString(R.string.notify_netmap_upload);
		case NotifyBean.UPLOAD_QUESSTION:
			return context.getString(R.string.notify_quesstion_upload);
		case NotifyBean.NET_MAP_ANSWERD:
			return context.getString(R.string.notify_netmap_answerd);
		case NotifyBean.MAP_FLOWER:
			return context.getString(R.string.notify_netmap_flower);
		case NotifyBean.MAP_EGG:
			return context.getString(R.string.notify_netmap_egg);
		case NotifyBean.NET_MAP_ADDCOMMENT:
			return context.getString(R.string.notify_netmap_addcomment);
		case NotifyBean.QUESSTION_ANSWERD:
			return context.getString(R.string.notify_quesstion_answerd);
		case NotifyBean.QUESSTION_FLOWER:
			return context.getString(R.string.notify_quesstion_flower);
		case NotifyBean.QUESSTION_EGG:
			return context.getString(R.string.notify_quesstion_egg);
		case NotifyBean.QUESSTION_MAP_ADDCOMMENT:
			return context.getString(R.string.notify_quesstion_addcomment);
		case NotifyBean.MESSAGE_RECEIVE:
			return context.getString(R.string.notify_message_receive);
		case NotifyBean.PUSH_MSG:
			return context.getString(R.string.notify_message_push);
		}

		return context.getString(R.string.notify_unknown);
	}

	/**
	 * 读取通知标题信息
	 * @param context
	 * @param bean
	 */
	public static String readNotifyTitle(Context context,NotifyBean bean) {
		switch (bean.getType()) {
		case NotifyBean.UPLOAD_NET_MAP:
			return context.getString(R.string.notify_netmap_upload_title);
		case NotifyBean.UPLOAD_QUESSTION:
			return context.getString(R.string.notify_quesstion_upload_title);
		case NotifyBean.NET_MAP_ANSWERD:
			return context.getString(R.string.notify_netmap_answerd_title);
		case NotifyBean.MAP_FLOWER:
			return context.getString(R.string.notify_netmap_flower_title);
		case NotifyBean.MAP_EGG:
			return context.getString(R.string.notify_netmap_egg_title);
		case NotifyBean.NET_MAP_ADDCOMMENT:
			return context.getString(R.string.notify_netmap_addcomment_title);
		case NotifyBean.QUESSTION_ANSWERD:
			return context.getString(R.string.notify_quesstion_answerd_title);
		case NotifyBean.QUESSTION_FLOWER:
			return context.getString(R.string.notify_quesstion_flower_title);
		case NotifyBean.QUESSTION_EGG:
			return context.getString(R.string.notify_quesstion_egg_title);
		case NotifyBean.QUESSTION_MAP_ADDCOMMENT:
			return context.getString(R.string.notify_quesstion_addcomment_title);
		case NotifyBean.MESSAGE_RECEIVE:
			return context.getString(R.string.notify_message_receive_title);
		case NotifyBean.PUSH_MSG:
			return context.getString(R.string.notify_message_push_title);
		}
		return context.getString(R.string.app_name);
	}

	/**
	 * 读取通知消息的intent
	 * @param context
	 */
	public static Intent readNotifyIntent(Context context,NotifyBean bean) {
		Intent intent = new Intent();
		Bundle extras = new Bundle();
		switch (bean.getType()) {
		case NotifyBean.UPLOAD_NET_MAP:
			intent.setClass(context, NetUserMapActivity.class);
			break;
		case NotifyBean.UPLOAD_QUESSTION:
			intent.setClass(context, QuesstionMapActivity.class);
			break;
		case NotifyBean.NET_MAP_ANSWERD:
		case NotifyBean.MAP_FLOWER:
		case NotifyBean.MAP_EGG:
		case NotifyBean.NET_MAP_ADDCOMMENT:
			extras.putInt("_id", Integer.parseInt(bean.getId()));
			intent.putExtras(extras);
			intent.setClass(context, ShowMapDetailActivity.class);
			break;
		case NotifyBean.QUESSTION_ANSWERD:
		case NotifyBean.QUESSTION_FLOWER:
		case NotifyBean.QUESSTION_EGG:
		case NotifyBean.QUESSTION_MAP_ADDCOMMENT:
			extras.putInt("_id", Integer.parseInt(bean.getId()));
			intent.putExtras(extras);
			intent.setClass(context, ShowAnswerMapDetailActivity.class);
			break;
		case NotifyBean.MESSAGE_RECEIVE:
			extras.putInt("_id", Integer.parseInt(bean.getId()));
			intent.putExtras(extras);
			intent.setClass(context, SiteMessageActivity.class);
			break;
		case NotifyBean.PUSH_MSG:
		default:
			return null;
		}

		intent.putExtra(NotifyBean.NOTIFY_TAG, bean.getType());
		//这个技巧会使得每个通知启动Activity的时候重新传递Extras，否则如果Activity相同，Extras不会被重新传递
		intent.setAction(String.valueOf(bean.getType()));
		return intent;
	}
}
